#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import gtk
import gobject
import gtk.glade
import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar

from gcapplet import globals
#import globals 

class EventWindow:
	def __init__(self, date, authorization):
		
		self.gladefile = globals.DATA_DIR  + "/events.glade"
		#self.gladefile = "events2.glade"

		self.gladefile = globals.DATA_DIR  + "/events.glade"
		#self.gladefile = "events.glade"

		self.wTree = gtk.glade.XML(self.gladefile, "window1")
		dic = {"on_MainWindow_destroy" : gtk.main_quit,
		 "on_add" : self.on_add, "on_del" : self.on_del, 
		 "on_edit" : self.on_edit}
		self.wTree.signal_autoconnect(dic)
		self.Events = self.wTree.get_widget("Events")
		
		self.List = gtk.ListStore(gobject.TYPE_PYOBJECT
				    , gobject.TYPE_STRING
									, gobject.TYPE_STRING
									, gobject.TYPE_STRING)
		self.create_column("Название", 1)
		self.create_column("Начало", 2)
		self.create_column("Конец", 3)
		
		self.Events.set_model(self.List)
		
		#self.calendar = self.calendar_init()
		self.calendar = authorization
		self.date = date
		print(date)
		end_date = date + "T23:59:59"
		
		self.query_events(self.calendar, date, end_date)
		
	def create_column(self, title, id):
		column = gtk.TreeViewColumn(title, gtk.CellRendererText(), text = id)
		column.set_sort_column_id(id)    
		self.Events.append_column(column)
	
	def on_add(self, widget):
		dlg = AddEditDlg(self.calendar, self.date)
		result, event = dlg.new_event()
		print(event)
		if (result == gtk.RESPONSE_OK and event):
			print('OK!')
			new_event = self.calendar.InsertEvent(event, '/calendar/feeds/default/private/full')
			for a_when in event.when:
				start = a_when.start_time
				end = a_when.end_time
			if start.split('T')[0] == self.date:
				self.List.append([event, event.title.text, start.replace('T',' ')[:16]
					, end.replace('T', ' ')[:16]])
	
	def on_del(self, widget):
		selection = self.Events.get_selection()
		model, iter = selection.get_selected()
		if iter:
			self.calendar.DeleteEvent(model[iter][0].GetEditLink().href)
			self.List.remove(iter)
	    
	    
	def on_edit(self, widget):
		self.wTree = gtk.glade.XML(self.gladefile, "add_edit_dlg")
		self.dlg = self.wTree.get_widget("add_edit_dlg")
		selection = self.Events.get_selection()
		model, iter = selection.get_selected()

		dlg = AddEditDlg(self.calendar)	
		
		self.title_entry = self.wTree.get_widget("title_entry")
		event = model[iter][0]
		previous_title = event.title.text
		title_text = self.title_entry.get_text()
		self.calendar.UpdateEvent(event.GetEditLink().href, event)
	
	#def calendar_init(self, username = 'developer221@gmail.com', password = 'developer'):
		#calendar = gdata.calendar.service.CalendarService()
		#calendar.email = username
		#calendar.password = password
		#calendar.source = 'Google-Calendar_Python_Sample-1.0'
		#calendar.ProgrammaticLogin()
		#return calendar
		
	def query_events(self, calendar, start_min, start_max):
		print(start_min)
		print(start_max)
		query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full')
		query.start_min = start_min
		query.start_max = start_max
		feed = calendar.CalendarQuery(query)
		
		for an_event in feed.entry:
			for a_when in an_event.when:
			    start = a_when.start_time
			    end = a_when.end_time 
			self.List.append([an_event, an_event.title.text, start.replace('T',' ')[:16]
			 , end.replace('T', ' ')[:16]])
		 
		 
class AddEditDlg:
	def __init__(self, calendar,date, event = None):
		self.calendar = calendar
		self.gladefile = globals.DATA_DIR +"/events.glade"
		#self.gladefile = "events2.glade"
		if event:
		    pass
		else: 
		    self.event = gdata.calendar.CalendarEventEntry()
		self.date = date
	
	def new_event(self):
		self.wTree = gtk.glade.XML(self.gladefile, "add_edit_dlg")
		self.dlg = self.wTree.get_widget("add_edit_dlg")
		
		
		self.result = self.dlg.run()
		self.title_entry = self.wTree.get_widget("title_entry")
		title_text = self.title_entry.get_text()
		print('title')
		print(title_text)
	
		if title_text:
			self.event.title = atom.Title(text=str(title_text))
		else:
			print('ВВЕДИ ЧОНИТЬ!')
		    
		self.content_entry = self.wTree.get_widget("content_entry")
		content_text = self.content_entry.get_text()
		print(content_text)
		
		if content_text:
			self.event.content = atom.Content(text=str(content_text))
		else:
			print('ВВЕДИ ЧОНИТЬ!')
		
		self.where_entry = self.wTree.get_widget("where_entry")
		where_text = self.where_entry.get_text()
		if where_text:
			self.event.where.append(gdata.calendar.Where(value_string=str(where_text)))
		else:
			print('ВВВВЕДИИИ')
		#start_text = None
		self.start_hours_entry = self.wTree.get_widget("spinbutton1")
		self.start_minutes_entry = self.wTree.get_widget("spinbutton2")
		print(self.start_hours_entry.get_text())
		print(self.start_minutes_entry.get_text())
		
		if int(self.start_hours_entry.get_text()) <= 9 and self.start_minutes_entry.get_text() == '0':
			start_text = self.date + 'T' + '0' + self.start_hours_entry.get_text() + ':' + self.start_minutes_entry.get_text() + '0' + ':' + '00'
		
		if self.start_minutes_entry.get_text() == '0' and int(self.start_hours_entry.get_text()) > 9:
			start_text = self.date + 'T' + self.start_hours_entry.get_text() + ':' + self.start_minutes_entry.get_text() + '0' + ':' '00'
			print(start_text)
		
		
		if int(self.start_hours_entry.get_text()) <= 9 and self.start_minutes_entry.get_text() != '0':
			start_text = self.date + 'T' + '0' + self.start_hours_entry.get_text() + ':' + self.start_minutes_entry.get_text() + ':' + '00'
			
		if int(self.start_hours_entry.get_text()) > 9 and self.start_minutes_entry.get_text() != '0':
			start_text = self.date + 'T' + self.start_hours_entry.get_text() + ':' + self.start_minutes_entry.get_text() + ':' + '00'

			
		
		self.end_hours_entry = self.wTree.get_widget("spinbutton3")
		self.end_minutes_entry = self.wTree.get_widget("spinbutton4")
		print(self.end_hours_entry.get_text())
		print(self.end_minutes_entry.get_text())
		
		if int(self.end_hours_entry.get_text()) <= 9 and self.end_minutes_entry.get_text() == '0':
			end_text = self.date + 'T' + '0' + self.end_hours_entry.get_text() + ':' + self.end_minutes_entry.get_text() + '0' + ':' + '00'
		
		if self.end_minutes_entry.get_text() == '0' and int(self.end_hours_entry.get_text()) > 9:
			end_text = self.date + 'T' + self.end_hours_entry.get_text() + ':' + self.end_minutes_entry.get_text() + '0' + ':' '00'
			print(start_text)
		
		
		if int(self.end_hours_entry.get_text()) <= 9 and self.end_minutes_entry.get_text() != '0':
			end_text = self.date + 'T' + '0' + self.end_hours_entry.get_text() + ':' + self.end_minutes_entry.get_text() + ':' + '00'
			
		if int(self.end_hours_entry.get_text()) > 9 and self.end_minutes_entry.get_text() != '0':
			end_text = self.date + 'T' + self.end_hours_entry.get_text() + ':' + self.end_minutes_entry.get_text() + ':' + '00'

		
		
		if start_text is None:
			start_text = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime()) #!!!!!!!!!!!!!!!!!!
			end_text = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 3600))
		
		self.event.when.append(gdata.calendar.When(start_time=start_text, end_time=end_text))
		
		
		self.dlg.destroy()
		return self.result, self.event
	
	def update_event(self):
		#self.wTree = gtk.glade.XML(self.gladefile, "add_edit_dlg")
		#self.dlg = self.wTree.get_widget("add_edit_dlg")
		
		#self.result = self.dlg.update_event()
		#self.title_entry = self.wTree.get_widget("title_entry")
		pass
		
		

	
#def test():
    #ui = EventWindow()
    #gtk.main()	    
	    
#if __name__ == '__main__':
	#test()
	
